﻿using TrueSync;
using UnityEngine;


namespace HEFramework
{
    public static class RoundSystem
    {
        [EntitySystem]
        public class RoundAwakeSystem : AwakeSystem<Round>
        {
            protected override void Awake(Round _self)
            {
                _self.Round_Counts = 0;
                _self.Reset_Round_Seconds();
                _self.Reset_Monster_Generate_Seconds();
            }
        }


        [EntitySystem]
        public class RoundUpdateSystem : UpdateSystem<Round>
        {
            protected override void Update(Round _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                var gTable = dc.LoadDataTable<DRGlobalSettings>();
                FP deltaTime = Time.deltaTime;

                int seconds = (int)_self.Round_Seconds;
                _self.Round_Seconds -= deltaTime;
                _self.Monster_Generate_Seconds -= deltaTime;
                //秒数变化
                if (seconds != (int)_self.Round_Seconds)
                {
                    var player = _self.GetSceneChild<Player>();
                    player.PerSecond();
                }

                //回合数增加处理
                if (_self.Round_Seconds <= 0)
                {
                    _self.Reset_Round_Seconds();
                    _self.Round_Counts += 1;
                    //广播回合增加事件
                    EventListener.Instance.PublishAsync(_self.GetScene() as Scene, new Event.Round_Add()
                    {
                        Round_Count = _self.Round_Counts,
                    });
                }


                //怪物生成处理
                if (_self.Monster_Generate_Seconds <= 0)
                {
                    _self.Reset_Monster_Generate_Seconds();
                    var factory = _self.GetSceneChild<Factory>();
                    if (factory != null)
                    {
                        var ids = _self.MonsterGenerateIDs;
                        int teamID = 1;
                        int index = Random.Range(0, ids.Count);
                        var e = factory.AddMonster(ids[index], teamID, factory.GetRandomMonsterGeneratePos());
                    }
                }
            }
        }


        public static void Reset_Round_Seconds(this Round _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            var table = dc.LoadDataTable<DRGlobalSettings>();
            _self.Round_Seconds = Formula.TM(table.GetData((int)eGlobalSetting.Round_Seconds).Value1);
        }


        public static void Reset_Monster_Generate_Seconds(this Round _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            var table = dc.LoadDataTable<DRGlobalSettings>();
            _self.Monster_Generate_Seconds = Formula.TM(table.GetData((int)eGlobalSetting.Monster_Generate_Seconds).Value1);
        }
    }
}